/* @created on 08 Oct 2010 - Upgraded to D7 on 02 Apr 2012 by tuan */
// This JS script is used for Simple Popup module

// Our object namespace
Drupal.simple_popup = {};
Drupal.simple_popup.client_top = null;

(function($){
  Drupal.behaviors.simple_popup = {
    attach: function (context, settings) {
      // Auto attach event to HTML elements:
      $("a[rel='simple_popup']").click(function(e) {
        Drupal.simple_popup.build($(this));
        e.stopPropagation();
        e.preventDefault();
      });

      // Close when clicking on overlay
      $("#simple_popup_overlay").click(function(e) {
        // Close only if not during "loading" time
        if ($("#simple_popup_overlay_loading").css("display") == 'none') {
          Drupal.simple_popup.close();
          e.stopPropagation();
          e.preventDefault();
        }
      });
            
      // Close popup panel
      $("#simple_popup_close").click(function(e) {
        Drupal.simple_popup.close();
        e.stopPropagation();
        e.preventDefault();
      });
    }
  };

  /*
   * Function to close the popup
   */
  Drupal.simple_popup.close = function() {
    // Clean content and hide off
    $("#simple_popup #simple_popup_content #simple_popup_content_iframe").attr("src", "about:blank");
    $("#simple_popup #simple_popup_content #simple_popup_content_iframe").bind('load', function() {
      // Hide
      $("#simple_popup").slideUp();
      $("#simple_popup_overlay").hide();
            
      // Unbind the event to avoid duplicated loads
      $(this).unbind('load');
    });
  };

  /*
   * Build and popup a div
   */
  Drupal.simple_popup.build = function(a_object) {
    // Return if simple_popup div object not found
    if (!$("#simple_popup").length) {return false;}
    
    // Disable scroll
    //$("body").css("overflow", "hidden");
    
    // Make background blur
    $("#simple_popup_overlay").css({
      "top": $(window).scrollTop() + "px", 
      "left": $(window).scrollLeft() + "px",
      "height": $(window).height(), 
      "width": $(window).width(),
      "zIndex": 1000
    }).fadeIn();
        
    //  We show a text "loading" so that user will be waiting
    $("#simple_popup_overlay_loading").css({
      "top": ($(window).scrollTop() + 200) + "px", 
      "left": ($(window).width() - $("#simple_popup_overlay_loading").width()) / 2 + $(window).scrollLeft() + "px",
      "zIndex": 1001
    }).show('fast');
        
    // Assign content into popup
    $("#simple_popup #simple_popup_content #simple_popup_content_iframe").attr("src", $(a_object).attr("href"));
        
    var iframe = $("#simple_popup #simple_popup_content #simple_popup_content_iframe");
        
    $("#simple_popup #simple_popup_content #simple_popup_content_iframe").load(function() {
      // Hide the "loading" text
      $("#simple_popup_overlay_loading").hide();
        
      // Resize the frame height
      //$(this).css("height", client_height);
      //$(this).css("width", client_width);

      // Calculate the position to be shown
      Drupal.simple_popup.client_top = parseInt(Drupal.settings['simple_popup']['simple_popup_top']);
      if (!Drupal.simple_popup.client_top) {
        Drupal.simple_popup.client_top = 50; // Default value
      }
            
      var top = (Drupal.simple_popup.client_top + $(window).scrollTop()) + "px";
      var left = ($(window).width() - $("#simple_popup").width()) / 2 + $(window).scrollLeft() +'px';
        
      $("#simple_popup").css({
        "top": top, 
        "left": left,
        "zIndex": 1001
      }).slideDown();
    });

    // Move the overlay when scrolling
    $(window).scroll(function() {
      $("#simple_popup_overlay").css('top', $(window).scrollTop() +'px');
      
      var top = Drupal.simple_popup.client_top + $(window).scrollTop();
      $("#simple_popup").css('top', top +'px');
    });
        
    return false;
  };

})(jQuery);

